package mybatis_plus;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author 王小波
 * @description:
 * @date 2023/4/27 16:03
 */
public class MybatisPlusCodeGenerator {

    public final static String fileDir = ".\\src\\main\\java";

    public final static List<String> tableNames = Arrays.asList("lease_bank_dic");
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://192.168.150.250:3306/bto-solar-order?tinyIntisBit=false&useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=true&serverTimezone=Asia/Shanghai",
                        "admin", "bto123")
                .globalConfig(builder -> {
                    builder.author("adrian") // 设置作者
                            .disableOpenDir()
                            .enableSwagger() // 开启 swagger 模式
                            .dateType(DateType.SQL_PACK)
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(fileDir); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);
                }))
                .packageConfig(builder -> {
                    builder.parent("com.bto.solar.work") // 设置父包名
//                            .moduleName("dao") // 设置父包模块名
                            .entity("entity")
                            .service("dao")
                            .serviceImpl("dao.impl")
                            .mapper("mapper")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder
//                            .addInclude("^sto_.*")// 设置需要生成的表名
                            .addInclude(tableNames)// 设置需要生成的表名
                            .entityBuilder()
                            .enableFileOverride()
                            .enableRemoveIsPrefix()
                            .enableChainModel()
                            .enableLombok()
                            .idType(IdType.INPUT)
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                            .enableFileOverride()  // 开启文件覆盖
                            .controllerBuilder()
                            .enableRestStyle()   // RestController
                            .mapperBuilder()
                            .enableFileOverride()
                            .enableBaseResultMap()  //开启 BaseResultMap
                            .enableBaseColumnList() //开启 BaseColumnList
                            .enableMapperAnnotation()
                    ;
//                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateConfig(builder -> {
                    //不生成entity
                    builder
//                            .disable(TemplateType.ENTITY)
//                            .disable(TemplateType.CONTROLLER)
                            .build();

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
